﻿using System;
using System.Data;
using System.Windows;
using System.Windows.Threading;
using SLS.Base;
using SLS.ExClassLib.RTIService;
using SLS.Document;
using SLS.ExClassLib.Event;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.ExThreading;
using SLS.ExClassLib.Message;
namespace SLS.Core.HLASimu
{
    public class HLAFederate : Federate
    {
        
        public HLAFederate()
           :base()
        {
        }
        public HLAFederate(string m_ClientType, string m_FEDName, object m_RTIServiceClient)
            :base(m_ClientType,m_FEDName,m_RTIServiceClient)
        {
       
        }
        public override void SendMessage(MessageEx msg)
        {
            try
            {
                base.SendMessage(msg);
                ContactRTIService.Instance.SendRTIMessage(msg, XRTIServiceClient); 
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public override void ReceiveMessage(MessageEx msg)
        {
            try
            {
                base.ReceiveMessage(msg);
                MessageEx msgA = EventDispatcher.Instance.MessagePool.Find(msg.MessageID_Req);
                if (msgA != null)
                {
                    msg = EndUpMessage(msgA, msg);
                }
                string Request = msg.Address;
                if (Request == "WFServiceFederate")
                    return;
                SLBC doc = null;//sun20121002
                doc = (SLBC)DocumentManagement.Instance.FindDocumentByAddress(Request,"");
                if (doc != null)
                {
                    Tracer.Verbose(Request, msg);
                    doc.ProcessRTIMessage(msg);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public override void work_ChangedWorkItemState(object sender, ChangedWorkItemStateEventArgs e)
        {
            base.work_ChangedWorkItemState(sender, e);
            lock (this)
            {
                ExWorkItem exWinWI = (ExWorkItem)e.WorkItem;
                WorkItemState state = exWinWI.State;
                if (state == WorkItemState.Completed)
                {
                    MessageEx msg = exWinWI.ThisMessage;
                    string JobType = msg.JobType;
                    if (JobType == "FED2RTI")
                    {
                        Application.Current.Dispatcher.BeginInvoke(m_SendMessage, DispatcherPriority.ContextIdle, msg);
                    }
                    if (JobType == "RTI2FED")
                    {
                        Application.Current.Dispatcher.BeginInvoke(m_ReceiveMessage, DispatcherPriority.ContextIdle, msg);
                    }
                }
            }
        }
        public override void MakeUpMessage(string CallerType, ref MessageEx msg, object Data = null, string Action = "")
        {
            base.MakeUpMessage(CallerType,ref msg,Data,Action);
            DataSet setEvent = new DataSet();
            switch (Action)
            {
                case "OnJoinFederation":
                    msg.setEvent = setEvent; 
                    break;
                case "OnJoinedFederation":
                break;
                default:
                    break;
            } 
        }
        public override MessageEx EndUpMessage(MessageEx msgA, MessageEx msgB)
        {
            base.EndUpMessage(msgA,  msgB);
            switch (msgB.JobID)
            {                
                case "OnJoinedFederation":
                    msgB.Address = msgA.Address;
                    break;
                default:
                    break;
            }
            return msgB;
        }
     
        public void BuildWorkQueue(MessageEx msg)
        {
            base.BuildWorkQueue(msg); 
        }
    }
}
